package cn.kshost.zqyg.user.service.impl;

import cn.kshost.zqyg.user.service.SmsService;
import cn.kshost.zqyg.user.util.SmsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Override
    public void sendRegisterSmsCode(String phone) {

        //判断是否存在验证 如果存在就结束发送
        String redisCode = redisTemplate.opsForValue().get("phone:code:" + phone);
        if (StringUtils.hasText(redisCode)) {
            return;
        }

        //给手机号发送短信
        String code = SmsUtils.sendCode(phone);
        //使用redis key为手机号 value为验证码 有效期为5分钟
        redisTemplate.opsForValue().set("phone:code:"+phone,code,5, TimeUnit.MINUTES);


    }
}
